<template>
	<view>
		<view v-if="post" class="flex flex-column margin-top" style="padding-bottom: 100px;">
			<!-- 帖子详情 -->
			<view class="flex flex-column margin-left">
				<view class="flex flex-row justify-between">
					<view class="flex flex-row" @click="gotoDetails(post.userId)">
					  <u-avatar :src="post.avatarUrl" shape="circle" size="46" mode="scaleToFill"></u-avatar>
						<view style="margin-left: 10px;"></view>
						<u--text :lines="1" color="gba(0, 0, 0, .9" block="ture" size="23" :text="post.username"></u--text>
					
					</view>
					<u-tag text="关注" type="success" shape="circle" icon="plus"></u-tag>
					
				</view>
				<view style="margin-top: 15px;"></view>
				<u--text color="black" size="25" :text="post.title"></u--text>
				<view style="margin-top: 5px;"></view>
				 <u--text :lines="2" color="black" size="18" lineHeight="28px" :text="post.content"></u--text>
				 <view style="margin-top: 8px;"></view>
				<u--text :text="'发布时间：'+formatCreatedAt(post.createdAt)"></u--text>
			</view>
			<!-- 帖子图片 -->
			<view v-for="(image, index) in post.imageUrls.split(',')" :key="index" class="padding">
				<image style="width:100%; height: 250px;"
					:src="`http://localhost:8090/file/user/${post.userId}/posts/${image}`"></image>
			</view>
			<!-- 评论列表 -->
			<view class="flex flex-column margin-left" v-for="(comment, index) in commentList" :key="index">
				<view class="flex flex-row" style="padding-bottom: 20px;">
					<u-avatar :src="comment.avatarUrl" shape="circle" mode="scaleToFill"></u-avatar>
					<view class="flex-column" style="margin-left: 5px;">
						<u--text :text="comment.username"></u--text>
						<view style="margin-top: 10px;"></view>
						<u--text :text="comment.content"></u--text>
						<view style="margin-top: 15px;"></view>
						<u--text :text="formatCreatedAt(comment.createdAt)"></u--text>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部操作栏 -->
		<view class="bottom-bar justify-around">
			<view >
				<u--input placeholder="说点什么" border="surround" style="width: 40%;" v-model="commentContent"></u--input>
			</view>
			
			<u-icon color="red" label='评论' size="28" @click="addComment"></u-icon>
			<u-icon name="heart" @click="likePost" :color="isLiked ? 'red' : 'grey'" :label='post.likeCount'
				labelPos='bottom' size="28"></u-icon>
		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				post: null,
				commentList: [],
				commentContent: '',
				isLiked: false,
				userId: null
			};
		},
		onLoad(options) {
			this.userId = uni.getStorageSync('userInfo')?.userId || null;
			this.getPostDetail(options.postId);
			this.getCommentList(options.postId);
			this.checkLikeStatus(options.postId, this.userId);
		},
		methods: {
			getPostDetail(postId) {
				uni.request({
					url: `http://localhost:8090/post/${postId}`,
					method: 'GET',
					success: (res) => {
						if (res.data.code === 200) {
							this.post = res.data.data;
						} else {
							uni.showToast({
								title: '获取帖子详情失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						uni.showToast({
							title: '请求失败：' + error.errMsg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			getCommentList(postId) {
				uni.request({
					url: `http://localhost:8090/post/${postId}/commentsList`,
					method: 'GET',
					success: (res) => {
						if (res.data.code === 200) {
							this.commentList = res.data.data;
						} else {
							uni.showToast({
								title: '获取评论列表失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						uni.showToast({
							title: '请求失败：' + error.errMsg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			
			likePost() {
				if (!this.userId) {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				uni.request({
					url: `http://localhost:8090/post/${this.post.postId}/like`,
					method: 'POST',
					data: { userId: this.userId },
					header: { 'Content-Type': 'application/x-www-form-urlencoded' },
					success: (res) => {
						if (res.data.code === 200) {
							this.isLiked = !this.isLiked;
							this.post.likeCount = res.data.likeCount;
								if(this.isLiked==true){
								uni.showToast({
									title: '用户已经点过赞了',
									icon: 'none',
									duration: 2000
								});
							}else {
								uni.showToast({
									title: '点赞成功',
									icon: 'success',
									duration: 2000
								});
								}
							this.getPostDetail(this.post.postId); // 重新获取帖子详情
						} else {
							uni.showToast({
								title: '点赞失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						uni.showToast({
							title: '请求失败：' + error.errMsg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			addComment() {
				if (!this.commentContent) {
					uni.showToast({
						title: '评论内容不能为空',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (!this.userId) {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				uni.request({
					url: `http://localhost:8090/post/${this.post.postId}/comment`,
					method: 'POST',
					data: { userId: this.userId, content: this.commentContent },
					header: { 'Content-Type': 'application/x-www-form-urlencoded' },
					success: (res) => {
						if (res.data.code === 200) {
							this.commentContent = '';
							uni.showToast({
								title: '评论成功',
								icon: 'success',
								duration: 2000
							});
							this.getCommentList(this.post.postId); // 重新获取评论列表
						} else {
							uni.showToast({
								title: '评论失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						uni.showToast({
							title: '请求失败：' + error.errMsg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			checkLikeStatus(postId, userId) {
				uni.request({
					url: `http://localhost:8090/likes/check/${userId}/${postId}`,
					method: 'GET',
					success: (res) => {
						console.log(res)
						if (res.data.code === 200) {
							this.isLiked = res.data.data;
						
						} else {
							uni.showToast({
								title: '获取点赞状态失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						uni.showToast({
							title: '请求失败：' + error.errMsg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			formatCreatedAt(time) {
				return time ? moment(time).format('YYYY-MM-DD') : '';
			},
			gotoDetails(userId){
				
				uni.navigateTo({
					url:`/pages/my/page/page?userId=${userId}`
				})
			},
			
		}
	};
</script>

<style>
	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background-color: #fff;
		padding: 10px;
		display: flex;
		box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
	}
</style>
